package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import modle.User;
import util.Inter;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
/**
 * 登入判断
 * @author WXE
 *
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    @Override 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		System.out.println(username);
		System.out.println(password);
		User user = Inter.inter(username, password);
		if(  user != null ) {
			request.setAttribute("account", username);
			request.getSession().setAttribute("user", user);
			request.getRequestDispatcher("Start.jsp").forward(request, response);
		}else {
			response.setContentType("text/html;charset=gb2312");  
	        PrintWriter out = response.getWriter();  
	        out.println("<html>");  
	        out.print("<script>alert('登入失败，用户名或密码错误！');window.location='Login.jsp';</script>");
	        out.println("<body>");  
	        out.println("</body>");  
	        out.println("</html>");  
	        out.flush();  
		}
		
		 
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
    @Override 
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
